
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int ins_name_pass()
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    else
    {
        fprintf(stderr, "Opened database successfully\n");
    }

    sql = "INSERT INTO user (name,passwd) "
          "VALUES ( 'aaa', '111'); ";

    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}

int creat_tab()
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;

    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }
    else
    {
        fprintf(stdout, "Opened database successfully\n");
    }

    /* Create SQL statement */
    // sql = "CREATE TABLE COMPANY("
    //       "ID INT PRIMARY KEY     NOT NULL,"
    //       "NAME           TEXT    NOT NULL,"
    //       "AGE            INT     NOT NULL,"
    //       "ADDRESS        CHAR(50),"
    //       "SALARY         REAL );";
    // 这里需要设置自增
    sql = "CREATE TABLE user("
          "ID INT  NOT NULL  PRIMARY KEY  autoincrement   ,"
          "NAME           TEXT    NOT NULL,"
          "passwd        TEXT  NOT NULL);";

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }
    else
    {
        fprintf(stdout, "Table created successfully\n");
    }
    sqlite3_close(db);
    return 0;
}


int show_all()
{
    sqlite3 *db;
    int rc;
    char *sql, **dbResult, *errMsg, *buff_p, *buff_p_1;
    char buff[100] = {0};
    int nRow = 0, nColumn = 0;
    /* Open database */
    rc = sqlite3_open("test.db", &db);
    if (rc)
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }

    sprintf(buff, "select *  from  user ");
    sql = buff;
    sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &errMsg);
    if (nRow == 0)
    {
        // sprintf(msg_masse_main, "查询错误数据库报错\n");
        sqlite3_close(db);
        return -1;
    }
    memset(buff, 0, sizeof(buff));
    char msg_temp[100] = {0};
    for (int i = 1; i <= nRow; i++)
    {
        // 各行的具体每个数据
        for (int j = 0; j < nColumn; j++)
        {
            memset(msg_temp, 0, 100);
            printf("%-5s\t", dbResult[i * nColumn + j]);
            sprintf(msg_temp, "%-5s\t", dbResult[i * nColumn + j]);
            // strcat(msg_masse_main, msg_temp);
        }
        // sprintf(msg_temp, "msg_masse_main 的长度%ld\t", strlen(msg_masse_main));
        // strcat(msg_masse_main, msg_temp);
        // strcat(msg_masse_main, "\n");
        printf("msg_temp %s \n", msg_temp);
    }

    // sprintf(msg_masse_main, "--%s", buff_p);
    return 0;
}


int main(){
    creat_tab();
    
}

